<?php
/*
  功能：系统设置 model
  作者：cxm
  时间：2019-01-11
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SystemConfig extends BaseModel {
//    use SoftDeletes;
//    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='system_config';  //绑定表
    public $timestamps = false;

    /**
     * 系统配置信息集合
     * @var array
     */
    private static $_configs = null;
    /**
     * 根据配置代码获取相关配置信息
     * @param  string $code 配置代码
     * @return string       配置信息
     */
    public static function getConfig($code)
    {
        if (self::$_configs === null) {
            self::$_configs = [];

            $configs = self::all();
            foreach ($configs as $config) {
                self::$_configs[$config->code] = $config->val;
            }
        }
        return self::$_configs[$code];
    }

    /**
     * 重置配置信息
     */
    public static function resetConfig()
    {
        self::$_configs = null;
    }

    /**
     * 选项名称
     */
    public function getDefaultNamesAttribute(){
        if(!isset($this->attributes['default_names']) || empty($this->attributes['default_names'])) {
            return [];
        }
        $default_names = $this->attributes['default_names'];
        $arr = explode(',',$default_names);
        return $arr;
    }

    public function getValAttribute(){
        if (!isset($this->attributes['val']) || empty($this->attributes['val'])) {
            return '';
        }

        $value = $this->attributes['val'];

        $value = htmlspecialchars_decode($value);

        return $value;
    }

    /**
     * 根据code获取内容
     * @param $code
     * @return mixed
     */
    static public function getGroupCodeData($code){
        $result = self::where('group_code', 'like', '%,' . $code . ',%')
            ->where('is_show', '1')
            ->orderBy("sort")
            ->get()
            ->keyBy('code')
            ->toArray();
        foreach ($result as $key => $val) {
            if (is_numeric($val['val'])) {
                $val['val'] = (integer) $val['val'];
            }
            if ($val['show_type'] == 'image') {
                $path                 = File::where('id', $val['val'])->value('path');
                $result[$key]['path'] = $path;
            }
        }
        return $result;
    }


    /**
     * 获取指定的配置
     * @param $code
     * @return mixed
     */
    static public function getSingleValue($code){
        return self::where('code',$code)->value('val');
    }

    /**
     * 获取指定的配置额类型
     * @param $code
     * @return mixed
     */
    static public function getSingleType($code){
        return self::where('code',$code)->value('show_type');
    }

    /**
     * 详情
     * @param $where
     * @return mixed
     */
    static public function detail($where){
        return self::where($where)->first();
    }

    /**
     * 查询分组信息存在文件中
     * @param $code
     * @return mixed
     */
    static public function getGroupCodeDataSaveFile($code){
        $data = self::where('group_code', 'like', '%,' . $code . ',%')
            ->where('is_show', '1')
            ->orderBy("sort")
            ->pluck('val','code')
            ->toArray();
        $str = var_export($data,TRUE);
        file_put_contents(base_path().'/config/'.$code.'.php',"<?php \r\n return ".$str.";");
    }

}